/*
 Xern Entity Relocation Network
 Copyright (C) 2011 Ronie Salgado <roniesalg@gmail.com>

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/

#ifndef XERN_DISPATCH_TABLE_HPP
#define XERN_DISPATCH_TABLE_HPP

#include <map>

namespace Xern
{
	namespace detail
	{
		struct NullArg {};

		template<typename Class, typename R=void, typename A1=NullArg, typename A2=NullArg>
		struct DispatchFunctionType
		{
			typedef Class ClassType;
			typedef R (Class::*MemberType) (A1, A2);
			typedef R (*GlobalType) (A1, A2);
		};

		template<typename Class, typename R, typename A1>
		struct DispatchFunctionType<Class, R, A1>
		{
			typedef Class ClassType;
			typedef R (Class::*MemberType) (A1);
			typedef R (*GlobalType) (A1);
		};

		template<typename FunctionType>
		class DispatchFunction
		{
		public:
			typedef typename FunctionType::ClassType ClassType;
			typedef typename FunctionType::MemberType MemberType;
			typedef typename FunctionType::GlobalType GlobalType;

			DispatchFunction()
				: memberFunction(NULL), globalFunction(NULL), object(NULL)
			{
			};

			DispatchFunction(MemberType method, ClassType *object)
				: memberFunction(method), globalFunction(NULL), object(object)
			{
			};

			DispatchFunction(GlobalType function)
				: memberFunction(NULL), globalFunction(function), object(NULL)
			{
			};

			template<typename A1>
			bool operator()(A1 a1) const
			{
				if(memberFunction)
				{
					(object->*memberFunction)(a1);
					return true;
				}
				else if(globalFunction)
				{
					globalFunction(a1);
					return true;
				}
				else
				{
					return false;
				}
			}

			template<typename A1, typename A2>
			bool operator()(A1 a1, A2 a2) const
			{
				if(memberFunction)
				{
					(object->*memberFunction)(a1, a2);
					return true;
				}
				else if(globalFunction)
				{
					globalFunction(a1, a2);
					return true;
				}
				else
				{
					return false;
				}
			}


		private:
			MemberType memberFunction;
			GlobalType globalFunction;
			ClassType *object;
		};

		template<typename Dispatcher>
		class DispatchTableBase
		{
		public:
			typedef typename Dispatcher::ClassType ClassType;
			typedef typename Dispatcher::GlobalType GlobalType;
			typedef typename Dispatcher::MemberType MemberType;

			typedef std::map<std::string, Dispatcher> Container;

			const Dispatcher &operator[](const std::string &name) const
			{
				typename Container::const_iterator it = data.find(name);
				if(it != data.end())
					return it->second;
				return nullDispatch;
			}

			void Add(const std::string &name, MemberType method, ClassType *object)
			{
				data.insert(std::make_pair(name, Dispatcher(method, object)));
			}

			void Add(const std::string &name, GlobalType function)
			{
				data.insert(std::make_pair(name, Dispatcher(function)));
			}

		private:
			Container data;
			Dispatcher nullDispatch;
		};
	}

	template<typename Class, typename R, typename A1 = void, typename A2 = void>
	class DispatchTable:
		public detail::DispatchTableBase<
			detail::DispatchFunction<
				detail::DispatchFunctionType<Class, R, A1, A2> > >
	{
	};

	template<typename Class, typename R, typename A1, typename A2>
	class DispatchTable<Class, R(A1, A2)>:
		public detail::DispatchTableBase<
			detail::DispatchFunction<
				detail::DispatchFunctionType<Class, R, A1, A2> > >
	{
	};

	template<typename Class, typename R, typename A1>
	class DispatchTable<Class, R(A1)>:
		public detail::DispatchTableBase<
			detail::DispatchFunction<
				detail::DispatchFunctionType<Class, R, A1> > >
	{
	};
};

#endif //XERN_DISPATCH_TABLE_HPP
